﻿package mortal.game.view.signUp.sevenDayLogin
{
    import extend.language.*;
    import flash.events.*;
    import mortal.common.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.scene3D.player.info.*;
    import mortal.game.view.signUp.sevenDayLogin.data.*;
    import mortal.game.view.signUp.sevenDayLogin.panel.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class SevenDayLoginController extends Controller
    {
        private var _lineOnModule:SevenDayLoginModule;
        public static var selectDay:int = -1;

        public function SevenDayLoginController()
        {
            return;
        }// end function

        override public function get view() : IView
        {
            if (_view == null)
            {
                _view = new SevenDayLoginModule();
                this._lineOnModule = _view as SevenDayLoginModule;
                _view.addEventListener(WindowEvent.SHOW, this.onShow);
                _view.addEventListener(WindowEvent.CLOSE, this.onClose);
            }
            return _view;
        }// end function

        public function onShow(event:Event) : void
        {
            this.addEvents(true);
            return;
        }// end function

        public function onClose(event:Event) : void
        {
            this.addEvents(false);
            return;
        }// end function

        override protected function initServer() : void
        {
            EventUtil.addSelfEvent(EventName.SevenDayLoginOpneModule, this.onOpenModule, true);
            Dispatcher.addEventListener(EventName.NavbarButtonUIRowInited, this.allowShowWin);
            Cache.instance.role.roleEntityInfo.addEventListener(EntityInfoEventName.UpdateLevel, this.levelUpgrade);
            EventUtil.addNetEvent(ServerCommand.SevenDayGetInfo, this.initData, true);
            EventUtil.addSelfEvent(EventName.SevenDayLoginUpdateReward, this.getRewardBk, true);
            return;
        }// end function

        private function addEvents(param1:Boolean) : void
        {
            EventUtil.addSelfEvent(EventName.SevenDayLoginGotReward, this.getSevenDayRewardHd, param1);
            return;
        }// end function

        private function onOpenModule(param1:Object) : void
        {
            this.view.show();
            return;
        }// end function

        private function allowShowWin(param1:Object) : void
        {
            this.checkLevelIcon();
            return;
        }// end function

        private function onGroupChangeHd(param1:Object) : void
        {
            if (SevenDayLoginUtil.hasNextDayIcon() == false)
            {
                EventUtil.addNetEvent(ServerCommand.GroupPlayerInfoChange, this.onGroupChangeHd, false);
                return;
            }
            var _loc_2:* = Cache.instance.role.entityInfo.level;
            var _loc_3:* = GameDefConfig.instance.getSystemOpenLevel(41);
            if (_loc_2 >= _loc_3 && _loc_2 <= _loc_3 + 3)
            {
                return;
            }
            if (Cache.instance.group.isInGroup)
            {
            }
            return;
        }// end function

        private function checkLevelIcon() : void
        {
            if (SevenDayLoginUtil.hasNextDayIcon())
            {
                this.onGroupChangeHd(null);
                EventUtil.addNetEvent(ServerCommand.GroupPlayerInfoChange, this.onGroupChangeHd, true);
            }
            else
            {
                EventUtil.addNetEvent(ServerCommand.GroupPlayerInfoChange, this.onGroupChangeHd, false);
            }
            var _loc_1:* = Cache.instance.role.entityInfo.level;
            var _loc_2:* = GameDefConfig.instance.getSystemOpenLevel(51);
            var _loc_3:* = GameDefConfig.instance.getSystemOpenLevel(41);
            if (_loc_1 >= _loc_3)
            {
                if (SevenDayLoginUtil.isInSevenDay())
                {
                    SevenDayLoginIcon.instance.show();
                    if (SevenDayLoginUtil.hasSevenDayReward())
                    {
                        SevenDayLoginIcon.instance.playEffect();
                    }
                    else
                    {
                        SevenDayLoginIcon.instance.stopEffect();
                    }
                }
                else if (SevenDayLoginUtil.hasSevenDayReward())
                {
                    SevenDayLoginIcon.instance.show();
                    SevenDayLoginIcon.instance.playEffect();
                }
                else if (SevenDayLoginIcon.hasInstance())
                {
                    SevenDayLoginIcon.instance.stopEffect();
                    SevenDayLoginIcon.instance.hide();
                }
            }
            else if (SevenDayLoginIcon.hasInstance())
            {
                SevenDayLoginIcon.instance.stopEffect();
                SevenDayLoginIcon.instance.hide();
            }
            return;
        }// end function

        private function levelUpgrade(event:Event) : void
        {
            this.checkLevelIcon();
            Cache.instance.sevenDayLogin.checkHasReward();
            return;
        }// end function

        private function initData(param1:Object) : void
        {
            var _loc_2:Object = null;
            if (this._lineOnModule && this._lineOnModule.sevenDayLoginPanel && Cache.instance.sevenDayLogin.rewardInfo)
            {
                _loc_2 = Cache.instance.sevenDayLogin.rewardInfo.giftInfos;
                this._lineOnModule.sevenDayLoginPanel.updateRewardStatus(_loc_2);
            }
            this.levelUpgrade(null);
            return;
        }// end function

        private function getSevenDayRewardHd(event:DataEvent) : void
        {
            var _loc_2:* = event.data as int;
            _loc_2++;
            if (this.checkBag(_loc_2))
            {
                MsgManager.showRollTipsMsg("" + Language.getString(71933));
            }
            else
            {
                GameProxy.sevenDayLogin.getSevenDayReward(_loc_2);
            }
            return;
        }// end function

        private function checkBag(param1:int) : Boolean
        {
            var _loc_2:* = SevenDayLoginUtil.getCodeByDay(param1);
            return Cache.instance.pack.backPackCache.isPackFull(_loc_2, 1);
        }// end function

        private function getRewardBk(event:DataEvent) : void
        {
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            var _loc_2:* = event.data as int;
            var _loc_3:* = Cache.instance.sevenDayLogin.rewardInfo.giftInfos[_loc_2];
            if (this._lineOnModule && this._lineOnModule.sevenDayLoginPanel)
            {
                this._lineOnModule.sevenDayLoginPanel.updateBtnByDay(_loc_2, _loc_3);
                this._lineOnModule.sevenDayLoginPanel.flyIcons();
            }
            if (SevenDayLoginUtil.hasGotAllGift())
            {
                if (SevenDayLoginIcon.hasInstance())
                {
                    SevenDayLoginIcon.instance.hide();
                    this.view.hide();
                }
            }
            if (SevenDayLoginIcon.hasInstance() && SevenDayLoginUtil.hasSevenDayReward() == false)
            {
                SevenDayLoginIcon.instance.stopEffect();
                this.view.hide();
            }
            if (SevenDayLoginUtil.hasNextDayIcon())
            {
                _loc_4 = Cache.instance.role.entityInfo.level;
                _loc_5 = GameDefConfig.instance.getSystemOpenLevel(41);
                if (_loc_4 >= _loc_5)
                {
                }
                EventUtil.addNetEvent(ServerCommand.GroupPlayerInfoChange, this.onGroupChangeHd, true);
            }
            else
            {
                EventUtil.addNetEvent(ServerCommand.GroupPlayerInfoChange, this.onGroupChangeHd, false);
                return;
            }
            return;
        }// end function

    }
}
